{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Using the Raw API\n",
    "\n",
    "This notebook walks through how to use the raw API and how"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--2024-02-02 11:11:39--  https://arxiv.org/pdf/1706.03762.pdf\n",
      "Resolving arxiv.org (arxiv.org)... 151.101.131.42, 151.101.3.42, 151.101.67.42, ...\n",
      "Connecting to arxiv.org (arxiv.org)|151.101.131.42|:443... connected.\n",
      "HTTP request sent, awaiting response... 200 OK\n",
      "Length: 2215244 (2.1M) [application/pdf]\n",
      "Saving to: ‘./attention.pdf’\n",
      "\n",
      "./attention.pdf     100%[===================>]   2.11M  --.-KB/s    in 0.08s   \n",
      "\n",
      "2024-02-02 11:11:39 (27.3 MB/s) - ‘./attention.pdf’ saved [2215244/2215244]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "!wget \"https://arxiv.org/pdf/1706.03762.pdf\" -O \"./attention.pdf\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "api_key = \"llx-...\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "import mimetypes\n",
    "import requests\n",
    "import time\n",
    "\n",
    "headers = {\"Authorization\": f\"Bearer {api_key}\"}\n",
    "file_path = \"./attention.pdf\"\n",
    "base_url = \"https://api.cloud.llamaindex.ai/api/parsing\"\n",
    "\n",
    "with open(file_path, \"rb\") as f:\n",
    "    mime_type = mimetypes.guess_type(file_path)[0]\n",
    "    files = {\"file\": (f.name, f, mime_type)}\n",
    "\n",
    "    # send the request, upload the file\n",
    "    url = f\"{base_url}/upload\"\n",
    "    response = requests.post(url, headers=headers, files=files)\n",
    "\n",
    "response.raise_for_status()\n",
    "# get the job id for the result_url\n",
    "job_id = response.json()[\"id\"]\n",
    "result_type = \"text\"  # or \"markdown\"\n",
    "result_url = f\"{base_url}/job/{job_id}/result/{result_type}\"\n",
    "\n",
    "# check for the result until its ready\n",
    "while True:\n",
    "    response = requests.get(result_url, headers=headers)\n",
    "    if response.status_code == 200:\n",
    "        break\n",
    "\n",
    "    time.sleep(2)\n",
    "\n",
    "# download the result\n",
    "result = response.json()\n",
    "output = result[result_type]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                          Provided proper attribution is provided, Google hereby grants permission to\n",
      "                          reproduce the tables and figures in this paper solely for use in journalistic or\n",
      "                                                                   scholarly works.\n",
      "                                                   Attention Is All You Need\n",
      "arXiv:1706.03762v7  [cs.CL]  2 Aug 2023\n",
      "                            Ashish Vaswani∗                Noam Shazeer∗               Niki Parmar∗             Jakob Uszkoreit∗\n",
      "                              Google Brain                  Google Brain             Google Research            Google Research\n",
      "                        avaswani@google.com             noam@google.com            nikip@google.com            usz@google.com\n",
      "                                Llion Jones∗                 Aidan N. Gomez∗        †                  Łukasz Kaiser∗\n",
      "                             Google Research                University of Toronto        \n"
     ]
    }
   ],
   "source": [
    "print(output[:1000])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "llama-parse-aNC435Vv-py3.11",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
